package client;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer;
import com.jme3.scene.shape.Sphere;
import java.util.ArrayList;

public class ObjectNPC 
{
    ArrayList<Geometry> npcs = new ArrayList<Geometry>();
    int lastthought;
    int curthought;
    FloatingText name;
    
    /*
    add_npc:
    appends the given node to the npc list
    //TASK: redo npc completely with non random searching
    */
    public void addNPC(Geometry node)
    {
        lastthought = (int)System.currentTimeMillis();
        System.out.println("Attempting append..");
        if(node != null)
        {
            System.out.println("NPC not null, Appending..");
            boolean add = npcs.add(node);
            if(add) 
            { 
                node.setName("NPC " + npcs.size());
                System.out.println("Appended NPC as : " + node.getName()); 
            }
        }
    }
    
    /*
    spawn_npc_noder:
    creates an ai based ball.
    */
    public void spawnNPCNoder(SimpleApplication app, Vector3f loc)
    {
        Sphere temp2 = new Sphere(10, 10, 6);
        Geometry gTemp = new Geometry("Box",temp2);
        Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
        mat.setBoolean("m_UseMaterialColors", true);
        mat.setColor("m_Diffuse",ColorRGBA.Red);
        gTemp.setMaterial(mat);
        gTemp.setLocalTranslation(loc);
        gTemp.setShadowMode(ShadowMode.Cast);
        ControlNpc npcon = new ControlNpc();
        gTemp.addControl(npcon);
        Main.rendernode.attachChild(gTemp);
        //gTempControl.setKinematic(true);
        //System.out.println(gTemp);
        addNPC(gTemp);
        name = new FloatingText("NPC",gTemp,10,10);
    }
    
    public void timeSync()
    {
        curthought = (int)System.currentTimeMillis();
    }
    
    /*
    debug_line:
    draws a line between the two given points with the given color
    */
    private void debugLine(Vector3f pos1, Vector3f pos2, ColorRGBA color)
    {
        if (pos1 != null) 
        {
            Mesh traceline = new Mesh();
            traceline.setMode(Mesh.Mode.Lines);
            traceline.setBuffer(VertexBuffer.Type.Position, 3, new float[]{pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z});
            traceline.setBuffer(VertexBuffer.Type.Index, 2, new short[]{0, 1});
            traceline.updateBound();
            traceline.updateCounts();
            Geometry traceGeo = new Geometry("line", traceline);
            Material mat = new Material(Main.appref.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
            mat.setColor("Color", color);
            traceGeo.setMaterial(mat);
            Main.appref.getRootNode().attachChild(traceGeo);
        }
    }
    
    /*
    isFree:
    checks if the given collision results are free
    */
    private boolean isFree(Geometry targ, Geometry obj)
    {
        CollisionResults results = new CollisionResults();
        int numCollisions = obj.collideWith(targ.getModelBound(), results);
        if (numCollisions > 0) 
        {
            for(int i = 0; i < numCollisions; i++)
            {
                //System.out.println(results.getCollision(i).getGeometry().getName());
                CollisionResult hit = results.getCollision(i);
            }
            //System.out.println("Pathing encountered a collision, raising.");
            return false;
        }
        else return true;
    }
}
